home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / KLOG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-31  |  3.0 KB  |  111 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/krb/RCS/klog.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  */
  11.  
  12. #ifndef lint
  13. static char *rcsid_klog_c =
  14. "$Header: klog.c,v 4.6 88/12/01 14:06:05 jtkohl Exp $";
  15. #endif /* lint */
  16.  
  17. #include <mit_copy.h>
  18. #include <time.h>
  19. #include <stdio.h>
  20.  
  21. #include <krb.h>
  22. #include <klog.h>
  23.  
  24. static char *log_name = KRBLOG;
  25. static int is_open;
  26. static char logtxt[1000];
  27.  
  28. /*
  29.  * This file contains two logging routines: kset_logfile()
  30.  * to determine the file to which log entries should be written;
  31.  * and klog() to write log entries to the file.
  32.  */
  33.  
  34. /*
  35.  * klog() is used to add entries to the logfile (see kset_logfile()
  36.  * below).  Note that it is probably not portable since it makes
  37.  * assumptions about what the compiler will do when it is called
  38.  * with less than the correct number of arguments which is the
  39.  * way it is usually called.
  40.  *
  41.  * The log entry consists of a timestamp and the given arguments
  42.  * printed according to the given "format" string.
  43.  *
  44.  * The log file is opened and closed for each log entry.
  45.  *
  46.  * If the given log type "type" is unknown, or if the log file
  47.  * cannot be opened, no entry is made to the log file.
  48.  *
  49.  * The return value is always a pointer to the formatted log
  50.  * text string "logtxt".
  51.  */
  52.  
  53. char * klog(type,format,a1,a2,a3,a4,a5,a6,a7,a8,a9,a0)
  54.     int type;
  55.     char *format;
  56.     int a1,a2,a3,a4,a5,a6,a7,a8,a9,a0;
  57. {
  58.     FILE *logfile, *fopen();
  59.     long time(),now;
  60.     char *month_sname();
  61.     struct tm *tm;
  62.     static int logtype_array[NLOGTYPE] = {0,0};
  63.     static int array_initialized;
  64.  
  65.     if (!(array_initialized++)) {
  66.         logtype_array[L_NET_ERR] = 1;
  67.         logtype_array[L_KRB_PERR] = 1;
  68.         logtype_array[L_KRB_PWARN] = 1;
  69.         logtype_array[L_APPL_REQ] = 1;
  70.         logtype_array[L_INI_REQ] = 1;
  71.         logtype_array[L_DEATH_REQ] = 1;
  72.         logtype_array[L_NTGT_INTK] = 1;
  73.         logtype_array[L_ERR_SEXP] = 1;
  74.         logtype_array[L_ERR_MKV] = 1;
  75.         logtype_array[L_ERR_NKY] = 1;
  76.         logtype_array[L_ERR_NUN] = 1;
  77.         logtype_array[L_ERR_UNK] = 1;
  78.     }
  79.  
  80.     (void) sprintf(logtxt,format,a1,a2,a3,a4,a5,a6,a7,a8,a9,a0);
  81.  
  82.     if (!logtype_array[type])
  83.     return(logtxt);
  84.  
  85.     if ((logfile = fopen(log_name,"a")) == NULL)
  86.         return(logtxt);
  87.  
  88.     (void) time(&now);
  89.     tm = localtime(&now);
  90.  
  91.     fprintf(logfile,"%2d-%s-%02d %02d:%02d:%02d ",tm->tm_mday,
  92.             month_sname(tm->tm_mon + 1),tm->tm_year,
  93.             tm->tm_hour, tm->tm_min, tm->tm_sec);
  94.     fprintf(logfile,"%s\n",logtxt);
  95.     (void) fclose(logfile);
  96.     return(logtxt);
  97. }
  98.  
  99. /*
  100.  * kset_logfile() changes the name of the file to which
  101.  * messages are logged.  If kset_logfile() is not called,
  102.  * the logfile defaults to KRBLOG, defined in "krb.h".
  103.  */
  104.  
  105. kset_logfile(filename)
  106.     char *filename;
  107. {
  108.     log_name = filename;
  109.     is_open = 0;
  110. }
  111.